﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AircraftCarrier.Lib;
using System.Timers;

namespace AircraftCarrier
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            curCapture = null;
            handler = new TradeHandle();
        }
        public TradeHandle handler;
        public FormCatcher curCapture { get; set; }
        public System.Timers.Timer t { get; set; }
        private void start_Click(object sender, EventArgs e)
        {
            FormBack captureForm = new FormBack();
            string resultText = string.Empty;
            captureForm.ShowDialog();
            this.curCapture = captureForm.catcher;
            IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
            string curFilename = string.Empty;
            string grayFilename = string.Empty;
            if (data.GetDataPresent(typeof(Bitmap)))
            {
                Bitmap curimage = (Bitmap)data.GetData(typeof(Bitmap));
                curimg.Image = curimage;
                int desWidth = curimage.Width + 100;
                int desHeight = curimage.Height + 100;
                System.Drawing.Bitmap timage = new System.Drawing.Bitmap(desWidth, desHeight);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(timage);
                g.Clear(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"));
                g.DrawImage(curimage, new System.Drawing.Rectangle(50, 50, curimage.Width, curimage.Height), new System.Drawing.Rectangle(0, 0, curimage.Width, curimage.Height), System.Drawing.GraphicsUnit.Pixel);
                curimg.Image = curimage;
                curFilename = Guid.NewGuid() + ".bmp";
                // timage.Save(curFilename);
                curimage.Save(curFilename);
            }
            try
            {
                resultText = OCRHelper.Process(curFilename);
            }
            catch (Exception ex)
            {
                //  MessageBox.Show("识别失败！");
            }
            finally
            {
                result.Text = resultText;
            }
        }

        private void Begin_Click(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            //实例化Timer类，设置间隔时间为10000毫秒； 
            t = new System.Timers.Timer(500);
            //到达时间的时候执行事件；
            t.Elapsed += new System.Timers.ElapsedEventHandler(UpdateData);
            t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)； 
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；

        }

        private void UpdateData(object sender, ElapsedEventArgs e)
        {
            if (curCapture != null)
            {
                Bitmap screen = Screenshot.GetCurScreenshot(curCapture.Location, curCapture.Size);
                double n1, n2, n3;
                string filename = Guid.NewGuid() + ".bmp";
                screen.Save(filename);
                string ocrresult = string.Empty;
                try
                {
                    ocrresult = OCRHelper.Process(filename);
                }
                catch (Exception ex)
                {

                }
                result.Text = ocrresult;
                n1 = ocrresult.ToDouble();
                n2 = handler.curPrice;
                n3 = n1 - n2;
                var updown = handler.Process(ocrresult.ToDouble());
                if (updown == ResultEnum.Down)
                {
                    traderesult.Text += "B";
                    
                }
                if (updown == ResultEnum.Up)
                {
                    traderesult.Text += "A";
                  
                }
                num_1.Text = n1.ToString();
                num_2.Text = n2.ToString();
                num_3.Text = n3.ToString();
                File.Delete(filename);
            }
            else
            {
                MessageBox.Show("请先选定区域");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            t.Stop();
        }
    }
}
